Skip to content

dasLLAMA load and per-call garbage, and a sink pass in the optimizer - #4783

Merged
borisbat merged 5 commits into
GaijinEntertainment:masterfrom
aleksisch:aleksisch/dasllama-load-leaks
Nov 25, 2026
Merged

borisbat merged 5 commits into
GaijinEntertainment:masterfrom
aleksisch:aleksisch/dasllama-load-leaks

Conversation

@aleksisch

@aleksisch aleksisch commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

dasLLAMA heap garbage at load and per call, and a sink pass in the optimizer.

Fixes, measured on the real models under -jit with force_inscope_pod:

  • dasLLAMA image: layout_fingerprint builds its description once
    (build_string) instead of a += chain.
  • dasLLAMA whisper: the load's staging is inscope (force_inscope_pod
    skips it: a user finalize) - 1.6 MB of heap garbage a base load, now 0.
  • dasLLAMA spm: spm_piece_of is [temp_string_result] and the encode
    reads its input as an inscope byte array - 18 KB of string garbage a
    Bergamot translate, now 0 (test_tokenizer_unigram_encode_no_garbage,
    +9200 bytes on the old code).

Tests: the optimizer's tests move to tests/optimization, registered as
an AOT suite of their own.

Compiler: optimizationSink (ast_sink.cpp, one visitor) moves a let into
the if right after it when all its uses sit in one arm. An inlined check
then builds its message only when it fails: 1M passing checks 118-259 ms
-> 4-25 ms interpreted, 96 ms -> <1 ms JIT. Initializer and condition
must be droppable. Policy disable_sink; AST serializer 289. Tests: 11, 4
fail under disable_sink. Full suite: same 14 failures + 1 error as base.

Compile cost, whole dasLLAMA engine (-compile-only -no-module-cache):
sink 90-96 ms (2.2% of optimizer time; DSE 590, CSE 850-915), ~0.5% of
the ~18 s compile; 2 extra optimizer rounds of 470.

Checks: lint 0, dasfmt, REVIEW.das, line length, tests/optimization
355/355 interpreted, JIT and AOT, tests/language 1943/1943.

@aleksisch
aleksisch force-pushed the aleksisch/dasllama-load-leaks branch 2 times, most recently from 9704ad2 to 8826ed9 Compare November 25, 2026 13:09
@aleksisch aleksisch changed the title dasLLAMA: model loads and TTS/ASR calls stop leaving heap garbage dasLLAMA image: the layout fingerprint writes its description once Nov 25, 2026
layout_describe built the meta closure's description with += on a
string: each append copies the whole description, and the copies stay
on the string heap until a collection - 226 MB a Kokoro load, 77 MB a
Pocket load. It is written into one build_string writer now: the same
text, so the same fingerprint, and every image on disk stays valid.

test_layout_fingerprint prints the Model closure a second time and holds
the string heap within 256 KB of where it stood.
@aleksisch
aleksisch force-pushed the aleksisch/dasllama-load-leaks branch from 8826ed9 to 61a71a0 Compare November 25, 2026 18:53
@aleksisch aleksisch changed the title dasLLAMA image: the layout fingerprint writes its description once dasLLAMA load and per-call garbage, and a sink pass in the optimizer Nov 25, 2026
stream_whisper_model's WhisperStaging was a plain local, so every load
left the staged planes on the heap until a collection: 1.6 MB of
garbage a Whisper base load. force_inscope_pod does not cover it - the
staging has a user finalize - so the local is inscope by hand. The load
leaves 0 KB of heap garbage.
The lattice built a fresh string for every candidate piece it looked up
and dropped it, and the encode built a dummy-space copy of its input:
460 bytes of string garbage an encode of a short sentence, 18 KB a
Bergamot translate. spm_piece_of is [temp_string_result], so the reclaim
pass frees each lookup key; the encode reads its input as an inscope
byte array (the space, then the text's bytes) and makes no string. The
same mark frees the piece the vocabulary reader rewrites.

test_tokenizer_unigram_encode_no_garbage holds twenty encodes to the
string heap one left (+9200 bytes before).
@aleksisch
aleksisch force-pushed the aleksisch/dasllama-load-leaks branch from 61a71a0 to 9001337 Compare November 25, 2026 19:10
The optimization*.das files, dead_branch_folding and the four rtti
uncategorized files that drive the inliner, CSE and fast-math move from
tests/language to tests/optimization with their eight helper modules.
The AOT build registers the folder as the optimization suite (its files
and its module helpers, both in test_aot_subset); the per-PR AOT run,
the fast-math runs and the sanitizer sweep take the folder beside
tests/language, so the moved tests keep every run they had. The two
paths utils/mcp and the lint skip fixture name follow the move.
@aleksisch
aleksisch force-pushed the aleksisch/dasllama-load-leaks branch 4 times, most recently from 259fb79 to bfb73f3 Compare November 25, 2026 20:56
optimizationSink (ast_sink.cpp) moves a one-variable let into the if
right after it when only one arm reads the variable and nothing after
the if does. The common case is an inlined check: the inliner binds the
message argument to a local ahead of the body's if, so a passing check
still built its message; sunk, it builds it only when it fails.

The initializer and the condition must both be droppable: no side
effect and no raise. A reference, inscope or collected local, a move
out of storage and an arm that shadows the name stay where they are.
The pass runs in the optimizer loop after dead stores. The policy
disable_sink turns it off; AST serializer version 289.

tests/optimization/optimization_sink.das: 11 tests, interpreted and
JIT; 4 fail under -policy disable_sink=true. Full suite: no change in
the failure set against the baseline. 1M passing checks: 125-147 ms ->
4 ms interpreted, 91-97 ms -> <1 ms JIT; benchmarks/core+micro and the
dasLLAMA loads unchanged.
@aleksisch
aleksisch force-pushed the aleksisch/dasllama-load-leaks branch from bfb73f3 to 11c552a Compare November 25, 2026 21:01
@aleksisch
aleksisch requested a review from borisbat November 25, 2026 22:08
@borisbat
borisbat merged commit 85bb689 into GaijinEntertainment:master Nov 25, 2026
66 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants